#pragma once

#include <QtWidgets/QMainWindow>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QProgressBar>
#include <QtWidgets/QListWidget>
#include <QtWidgets/QLabel>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QSplitter>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QAction>
#include <QtCore/QTimer>
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <memory>

#include "../Common/ScannerInterface.h"

#ifdef _WIN32
    #include <windows.h>
#else
    typedef void* HMODULE;
    typedef void* HANDLE;
#endif

class ScannerUI : public QMainWindow {
    Q_OBJECT

public:
    explicit ScannerUI(QWidget *parent = nullptr);
    ~ScannerUI();

private slots:
    void onBrowseFolder();
    void onStartScan();
    void onStopScan();
    void onClearResults();
    void onExportResults();
    void onAbout();
    void updateUI();
    void onFileItemClicked(QListWidgetItem* item);

private:
    // UI组件
    QWidget* centralWidget;
    QVBoxLayout* mainLayout;
    QSplitter* mainSplitter;
    
    // 控制面板
    QGroupBox* controlGroup;
    QGridLayout* controlLayout;
    QLineEdit* pathEdit;
    QPushButton* browseButton;
    QPushButton* startButton;
    QPushButton* stopButton;
    QPushButton* clearButton;
    QPushButton* exportButton;
    
    // 进度显示
    QGroupBox* progressGroup;
    QVBoxLayout* progressLayout;
    QProgressBar* progressBar;
    QLabel* statusLabel;
    QLabel* filesCountLabel;
    QLabel* totalSizeLabel;
    QLabel* currentFileLabel;
    
    // 结果显示
    QGroupBox* resultsGroup;
    QVBoxLayout* resultsLayout;
    QListWidget* fileList;
    QTextEdit* detailsText;
    
    // 菜单和状态栏
    QMenuBar* menuBar;
    QStatusBar* statusBar;
    QAction* aboutAction;
    QAction* exitAction;
    
    // DLL相关
    HMODULE scannerDll;
    HANDLE scanner;
    
    // 函数指针
    typedef HANDLE (*CreateScannerFunc)();
    typedef void (*DestroyScannerFunc)(HANDLE);
    typedef bool (*StartScanFunc)(HANDLE, const wchar_t*);
    typedef void (*StopScanFunc)(HANDLE);
    typedef bool (*IsScanningFunc)(HANDLE);
    typedef void (*SetProgressCallbackFunc)(HANDLE, ProgressCallbackC);
    typedef void (*SetCompleteCallbackFunc)(HANDLE, CompleteCallbackC);
    typedef int (*GetScanStatusFunc)(HANDLE);
    typedef uint64_t (*GetTotalFilesScannedFunc)(HANDLE);
    typedef uint64_t (*GetTotalBytesScannedFunc)(HANDLE);
    
    CreateScannerFunc createScannerFunc;
    DestroyScannerFunc destroyScannerFunc;
    StartScanFunc startScanFunc;
    StopScanFunc stopScanFunc;
    IsScanningFunc isScanningFunc;
    SetProgressCallbackFunc setProgressCallbackFunc;
    SetCompleteCallbackFunc setCompleteCallbackFunc;
    GetScanStatusFunc getScanStatusFunc;
    GetTotalFilesScannedFunc getTotalFilesScannedFunc;
    GetTotalBytesScannedFunc getTotalBytesScannedFunc;
    
    // 状态管理
    QTimer* updateTimer;
    QMutex dataMutex;
    
    // 扫描结果
    struct UIFileInfo {
        QString filePath;
        uint64_t fileSize;
        QString sizeString;
        
        UIFileInfo(const QString& path, uint64_t size) 
            : filePath(path), fileSize(size) {
            formatSize();
        }
        
    private:
        void formatSize();
    };
    
    std::vector<UIFileInfo> scanResults;
    QString currentScannedFile;
    uint64_t totalFiles;
    uint64_t totalBytes;
    bool isScanning;
    
    // 私有方法
    void setupUI();
    void setupMenuBar();
    void setupStatusBar();
    void connectSignals();
    bool loadScannerDLL();
    void unloadScannerDLL();
    bool initializeScanner();
    void cleanupScanner();
    void updateProgressInfo();
    void updateFileList();
    void enableControls(bool enabled);
    QString formatFileSize(uint64_t bytes);
    QString getFileDetails(const QString& filePath);
    
    // 静态回调函数
    static void progressCallback(const wchar_t* filePath, uint64_t fileSize, uint64_t totalFiles);
    static void completeCallback(const ScanResult* results, int count, int status);
    
    // 静态实例指针（用于回调）
    static ScannerUI* instance;
};